<?php
/**
 *
 */
namespace app\manage\controller\bank;

use app\manage\controller\Base;

class Handle extends Base
{
    private $params;
    public function __construct(){
        parent::__construct();
        // token验证
        if(false == Base::checkToken()){
            $this->error('token error!');
        }
        // 参数去token
        $params = $this -> request -> param();
        unset($params['token']);
        $this->params = $params;
    }

    public function add(){
        $params = $this->params;
        
        $bank_name = isset($params['bank_name'])?$params['bank_name']:'';
        if(!$bank_name)
            $this->error('请填写银行名称');

        $data = [
            'bank_name' => $bank_name,
        ];

        $model = model('bank');
        if($model->where($data)->find()){
            $this->error('该银行已经存在');
        }else{
            if($model->allowField(true)->save($data)){
                $CACHE = \app\common\cache\CacheData::getInstance('Bank');
                $CACHE -> updateCache();
                $this->success('添加成功');
            }else{
                $this->error('添加失败');
            }
        }
    }

    public function change(){
        $params = $this->params;
        
        $id = isset($params['id'])?$params['id']:0;
        if(!$id)
            $this->error('id错误');

        $status = isset($params['status'])?intval($params['status']):0;

        $model = model('bank');
        $bank = $model -> find($id);
        if(!$bank){
            $this->error('银行不存在');
        }
        $model->where('id',$id)->update(['status'=>$status]);

        $CACHE = \app\common\cache\CacheData::getInstance('Bank');
        $CACHE -> updateCache();
        $this->success('操作成功');
    }

}